<?php

/**
 * +---------------------------------------------------------------
 * | Author: 北京永青宇创科技有限公司
 * | Date: 2020/03/12
 * | 版本: 6.0
 * +---------------------------------------------------------------
 * | CopyRight: 北京永青宇创科技有限公司 All rights reserved.
 * +---------------------------------------------------------------
 * | 版权声明：云豹不是一个自由软件，是云豹官方推出的商业源码。
 * | 严禁在未经许可的情况下拷贝、复制、传播、使用云豹的任意代码
 * | 如有违反，请立即删除，否则您将面临承担相应，法律责任的风险。
 * | 如果需要取得官方授权，请联系官方 https://www.gepardshop.com
 * +---------------------------------------------------------------
 */

namespace app\ticket\controller;

use app\common\controller\Frontend;


class TicketHome extends Frontend {
	protected $noNeedLogin = ['*'];
	protected $noNeedRight = ['*'];
	protected $ticketModel;
	protected $ticketStaffModel;
	protected $ticketStatusModel;
	protected $ticketRoleModel;
	protected $ticketMessageModel;
	protected $ticketCategoryModel;
	protected $userModel;

	protected function initialize()
	{
		parent::initialize(); // TODO: Change the autogenerated stub
		$this->ticketModel         = model('ticket/Ticket');
		$this->ticketStaffModel    = model('ticket/TicketStaff');
		$this->ticketStatusModel   = model('ticket/TicketStatus');
		$this->ticketMessageModel  = model('ticket/TicketMessage');
		$this->ticketCategoryModel = model('ticket/TicketCategory');
		$this->userModel           = model('common/user');

		$this->assign('meta_title', '工单系统');
		$this->assign('site_name',  ' 工单系统');
		$this->ticketNav();
		//流程步骤
		$this->assign_steps();
		$this->set_step(1);

	}
	public function set_step($current = 1)
	{
		$this->currend_step = $current;
		$this->assign('current_step', $this->currend_step);
	}

	public function assign_steps()
	{
		$this->instal_steps = [
			'1' => ['id' => 1, 'title' => '选择问题所属产品'],
			'2' => ['id' => 2, 'title' => '选择问题类型'],
			'3' => ['id' => 3, 'title' => '推荐解决方案'],
			'4' => ['id' => 4, 'title' => '创建工单'],
		];

		$this->assign('install_steps', $this->instal_steps);

	}
	//顶部导航
	private function ticketNav()
	{

		//显示不同的顶部导航
		if(empty($this->user_info['group_id'])){
			$this->user_info['group_id']=1;
		}
		switch($this->user_info['group_id']) {

			case '11':
				$this->custom_nav = [
					[
						'url'   => url('ticket/index/index'),
						'title' => '首页',
						'class' => '',
					],
					[
						'url'   => url('ticket/manage.staff/index'),
						'title' => '员工管理',
						'class' => '',
						'child' => [
							[
								'url'   => url('ticket/manage.staff/index'),
								'title' => '员工管理',
								'class' => '',
							], [
								'url'   => url('ticket/manage.staff/edit'),
								'title' => '新增员工',
								'class' => '',
							],
						],
					],
					[
						'url'   => url('ticket/manage.ticket/allot'),
						'title' => '分配工单',
						'class' => '',
					],
					[
						'url'   => url('ticket/manage.product/index'),
						'title' => '产品管理',
						'class' => '',
						'child' => [
							[
								'url'   => url('ticket/manage.product/index'),
								'title' => '产品管理',
								'class' => '',
							], [
								'url'   => url('ticket/manage.product/edit'),
								'title' => '新增产品',
								'class' => '',
							],
						],
					],
					[
						'url'   => url('ticket/manage.category/index'),
						'title' => '问题分类管理',
						'class' => '',
						'child' => [
							[
								'url'   => url('ticket/manage.category/index'),
								'title' => '分类管理',
								'class' => '',
							],
							[
								'url'   => url('ticket/manage.category/edit'),
								'title' => '添加分类',
								'class' => '',
							],
						],
					],
					[
						'url'   => url('ticket/manage.solution/index'),
						'title' => '解决方案管理',
						'class' => '',
						'child' => [
							[
								'url'   => url('ticket/manage.solution/index'),
								'title' => '解决方案管理',
								'class' => '',
							], [
								'url'   => url('ticket/manage.solution/edit'),
								'title' => '解决方案新增',
								'class' => '',
							],
						],
					],
					[
						'url'   => url('ticket/user/index'),
						'title' => '个人中心',
						'class' => '',
					],
				];
				break;
			case '21':
				$this->custom_nav = [
					[
						'url'   => url('ticket/index/index'),
						'title' => '首页',
						'class' => '',
					],
					[
						'url'   => url('ticket/manage.message/index'),
						'title' => '消息',
						'class' => '',
					],
					[
						'url'   => url('ticket/manage.ticket/index'),
						'title' => '工单管理',
						'class' => '',
						'child' => [
							[
								'url'   => url('ticket/manage.ticket/wait'),
								'title' => '带处理工单',
								'class' => '',
							], [
								'url'   => url('ticket/manage.ticket/dispose'),
								'title' => '处理中工单',
								'class' => '',
							], [
								'url'   => url('ticket/manage.ticket/finish'),
								'title' => '已处理工单',
								'class' => '',
							],[
								'url'   => url('ticket/manage.ticket/makeover'),
								'title' => '已转让工单',
								'class' => '',
							],
						],
					],
					[
						'url'   => url('ticket/user/index'),
						'title' => '个人中心',
						'class' => '',
					],
				];
				break;
			default:
				$this->custom_nav = [
					[
						'url'   => url('ticket/index/index'),
						'title' => '首页',
						'class' => '',
					],
					[
						'url'   => url('ticket/manage.ticket/index'),
						'title' => '历史工单',
						'class' => '',
					],
					[
						'url'   => url('ticket/user/index'),
						'title' => '个人中心',
						'class' => '',
					],
				];
				break;
		}

		//定制各角色独立导航菜单
		if( ! empty($this->custom_nav)) {
			$this->assign('menu_list', $this->custom_nav);
		}
	}

	protected function assignDetail($id)
	{
		$ticket_info                 = $this->ticketModel->find($id);
		$custom_info                 = $this->userModel->find($ticket_info['custom_uid']);
		$ticket_info['custom_name']  = $custom_info['username'];
		$ticket_info['custom_email'] = $custom_info['email'];
		$this->assign('info', $ticket_info);
	}

	protected function assignStaff()
	{
		$map        = ['status' => 1];
		$staff_list = $this->ticketStaffModel->where($map)->select();
		foreach($staff_list as &$staff) {
			$staff_info          = $this->userModel->find($staff['uid']);
			$staff['staff_name'] = $staff_info['username'];
		}
		$this->assign('staff_list', $staff_list);
	}

	protected function assignStatus()
	{
		$map         = [];
		$status_list = $this->ticketStatusModel->where($map)->select();
		$this->assign('status_list', $status_list);
	}

	protected function assignMessage($id)
	{
		$map          = ['ticket_id' => $id, 'status' => 1];
		$message_list = $this->ticketMessageModel->where($map)->select();
		$this->assign('message_list', $message_list);
	}
}